import { setStore, getStore, removeStore } from '../../utils/store'
const state = {
    cart: {},
}

// mutations
const mutations = {
    //加入购物车，所需6个参数，商铺id，食品id，食品规格id，食品名字，食品价格，食品规格
    ADD_CART(state, {
        shopid,
        goods_id,
        sku_id,
        name,
        num,
        image,
        price,
        specs,
        checked
    }) {
        let cart = state.cart;
        let shop = cart[shopid] = (cart[shopid] || {});
        let item = shop[goods_id] = (shop[goods_id] || {});
        if (item[sku_id]) {
            item[sku_id]['num']++;
        } else {
            item[sku_id] = {
                "num": num,
                "id": sku_id,
                "name": name,
                "image": image,
                "price": price,
                "specs": specs,
                "checked": true
            };
        }
        state.cart = { ...cart };
        //存入localStorage
        setStore('buyCart', state.cart);
    },

    // 减少商品
    SUB_CART(state, {
        shopid,
        goods_id,
        sku_id,
        name,
        image,
        price,
        specs,
    }) {
        let cart = state.cart;
        let shop = cart[shopid] = (cart[shopid] || {});
        let item = shop[goods_id] = (shop[goods_id] || {});
        if (item && item[sku_id]) {
            if (item[sku_id]['num'] > 0) {
                item[sku_id]['num']--;
                state.cart = { ...cart };
                //存入localStorage
                setStore('buyCart', state.cart);
            } else {
                item[sku_id] = null;
            }

        }
    },

    // 根据商品勾选修改购物车
    EDIT_CART(state, {
        shopid,
        goods_id,
        sku_id,
        checked
    }) {
        let cart = state.cart;
        let shop = cart[shopid]
        let item = shop[goods_id]
        if (item && item[sku_id]) {
            item[sku_id]['checked'] = checked
            state.cart = { ...cart };
            //存入localStorage
            setStore('buyCart', state.cart);

        }
    },

    DELETE_CART(state, {
        shopid,
        goods_id,
        sku_id,
    }) {
        let cart = state.cart;
        let shop = cart[shopid]
        let item = shop[goods_id]
        if (item && item[sku_id]) {
            delete (item[sku_id])
            state.cart = { ...cart };
            //存入localStorage
            setStore('buyCart', state.cart);

        }
    },
    // 全选
    CHECK_ALL(state, checked) {
        let cart = state.cart;
        Object.keys(cart).forEach(shop_id => {
            let shop = cart[shop_id]
            Object.keys(shop).forEach(goods_id => {
                Object.values(shop[goods_id]).forEach(el => {
                    el.checked = checked
                })
            })
        })

        setStore('buyCart', state.cart);
    },

    // 店铺全选
    CHOOSE_SHOP(state, { checked, shop_id }) {
        let cart = state.cart;
        let shop = cart[shop_id]
        Object.keys(shop).forEach(goods_id => {
            Object.values(shop[goods_id]).forEach(el => {
                el.checked = checked
            })
        })

        setStore('buyCart', state.cart);
    },


    INIT_BUYCART(state) {
        let initCart = getStore('buyCart');
        if (initCart) {
            state.cart = JSON.parse(initCart);
        }
    },
    CLEAR_CART(state) {
        state.cart = []
        removeStore('buyCart')
    },
}

// actions
const actions = {

}

// getters
const getters = {

}



export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}